package com.yc.sole.erp.biz;

import cn.hutool.core.bean.BeanUtil;
import com.yc.sole.erp.dto.MaterialDto;
import com.yc.sole.erp.repository.MaterialRepository;
import com.yc.sole.erp.request.MaterialQueryReq;
import com.yc.sole.erp.request.MaterialReq;
import com.yc.sole.erp.response.MaterialResp;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.mybatis.util.PageUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * 服务接口实现
 *
 * @author yizuomin
 * @date 2024-01-24
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class MaterialBiz {

    private final MaterialRepository materialRepository;

    /**
     * 新增
     *
     * @param materialReq
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveMaterial(MaterialReq materialReq) {
        MaterialDto dto = new MaterialDto();
        BeanUtil.copyProperties(materialReq, dto);
        materialRepository.saveMaterial(dto);
    }

    /**
     * 修改
     *
     * @param materialReq
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateMaterialById(MaterialReq materialReq) {
        MaterialDto dto = new MaterialDto();
        BeanUtil.copyProperties(materialReq, dto);
        materialRepository.updateMaterialById(dto);
    }

    /**
     * 通过id批量删除
     *
     * @param ids
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteMaterialByIds(Collection<Long> ids) {
        materialRepository.deleteMaterialByIds(ids);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public MaterialResp selectMaterialById(Long id) {
        return BeanUtil.copyProperties(materialRepository.selectMaterialById(id), MaterialResp.class);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param materialQueryReq
     * @return
     */
    public Paging<MaterialResp> selectMaterialPage(Paging<MaterialQueryReq> page, MaterialQueryReq materialQueryReq) {
        Paging<MaterialDto> paging = materialRepository.selectMaterialPage(page, materialQueryReq);
        return PageUtil.pageCopy(paging, MaterialResp.class);
    }

    /**
     * 根据名称查询
     *
     * @param name
     * @return
     */
    public List<MaterialResp> selectMaterialByName(String name) {
        List<MaterialDto> materialDtoList = materialRepository.selectMaterialByName(name);
        return BeanUtil.copyToList(materialDtoList, MaterialResp.class);
    }
}
